package com.jz.community.moduleshoppingguide.home.ui.adapter;

import android.app.Activity;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.bean.baseCoupon.BaseCouponInfo;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.task.GetReceiveCouponTask;
import com.jz.community.basecomm.utils.BaseUserUtils;
import com.jz.community.basecomm.utils.CharacterUtils;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.home.bean.ChunnelNewbornBean;

import java.util.List;

public class NewbornAdapter extends BaseQuickAdapter<ChunnelNewbornBean.EmbeddedBean.ContentBean, BaseViewHolder> {


    public NewbornAdapter(int layoutResId, @Nullable List<ChunnelNewbornBean.EmbeddedBean.ContentBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ChunnelNewbornBean.EmbeddedBean.ContentBean item) {
        ImageView personImg = helper.getView(R.id.iv_new_person_img_one);
        Glide.with(mContext)
                .load(item.getIcon())
                .into(personImg);
        personImg.setOnClickListener(v ->
                ARouter.getInstance()
                        .build(RouterIntentConstant.MODULE_SHOPPING_GOODS_DETAIL)
                        .withString("url", item.getLinks())
                        .navigation());
        LinearLayout ll_new_person_det = helper.getView(R.id.ll_new_person_det);
        ll_new_person_det.setOnClickListener(v ->
                ARouter.getInstance()
                        .build(RouterIntentConstant.MODULE_SHOPPING_GOODS_DETAIL)
                        .withString("url", item.getLinks())
                        .navigation());
        TextView goodsName = helper.getView(R.id.tv_discount_goods_name);
        goodsName.setText(item.getTitle());
        TextView discountGoodsPrice = helper.getView(R.id.tv_discount_goods_new_price);
        TextView discountGoodsOldPrice = helper.getView(R.id.tv_discount_goods_old_price);
        if (!TextUtils.isEmpty(item.getAfterPrice())) {
            discountGoodsPrice.setText(CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(item.getAfterPrice())));
            discountGoodsOldPrice.setText(CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(item.getPrice())));
        } else {
            discountGoodsPrice.setText(CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(item.getPrice())));
            discountGoodsOldPrice.setText("");
        }
        TextView discountPrice = helper.getView(R.id.tv_discount_price);
        TextView symbol = helper.getView(R.id.tv_new_person_money_symbol);
        if (!TextUtils.isEmpty(item.getCouponAmount())) {
            symbol.setVisibility(View.VISIBLE);
            discountPrice.setText(((int) ConverterUtils.toDouble(item.getCouponAmount())) + "");
        }
        if (!TextUtils.isEmpty(item.getCouponDiscount())) {
            symbol.setVisibility(View.GONE);
            discountPrice.setText(((int) ConverterUtils.toDouble(item.getCouponDiscount())) + "折");
        }
        TextView tvGetCoupon = helper.getView(R.id.tv_get_coupon);

        LinearLayout ll_new_person_coupon = helper.getView(R.id.ll_new_person_coupon);
        LinearLayout ll_new_person_dash_bolder = helper.getView(R.id.ll_new_person_dash_bolder);
        TextView tv_new_person_money_symbol = helper.getView(R.id.tv_new_person_money_symbol);
        TextView tv_new_person_coupon_describe = helper.getView(R.id.tv_new_person_coupon_describe);
        TextView tv_new_person_coupon_line = helper.getView(R.id.tv_new_person_coupon_line);

        if (!TextUtils.isEmpty(item.getCouponId())) {
            if (item.getIsReceive() == 0) {
                tvGetCoupon.setOnClickListener(v -> {
                    if (BaseUserUtils.getIsLogin(mContext)) {
                        getReceiveCouponTask(item.getCouponId());
                    } else {
                        ARouter.getInstance().build(RouterIntentConstant.MODULE_AUTHORIZATION_LOGIN).navigation();
                    }
                });
            } else {
                ll_new_person_coupon.setBackground(mContext.getResources().getDrawable(R.drawable.bg_person_discount_all_gray_light_no_bolder_radius));
                ll_new_person_dash_bolder.setBackground(mContext.getResources().getDrawable(R.drawable.bg_dash_line_gray_light_bolder_radius));
                tvGetCoupon.setText("已领取过");
                tvGetCoupon.setBackground(mContext.getResources().getDrawable(R.drawable.bg_person_discount_gray_light_no_bolder_radius));
                tv_new_person_money_symbol.setTextColor(mContext.getResources().getColor(R.color.font_gary_999));
                tv_new_person_coupon_describe.setTextColor(mContext.getResources().getColor(R.color.font_gary_999));
                tv_new_person_coupon_line.setBackground(mContext.getResources().getDrawable(R.color.font_gary_999));
                discountPrice.setTextColor(mContext.getResources().getColor(R.color.font_gary_999));
                symbol.setTextColor(mContext.getResources().getColor(R.color.font_gary_999));
            }
        } else {
            ll_new_person_coupon.setVisibility(View.GONE);
        }
    }

    private void getReceiveCouponTask(String couponId) {
        new GetReceiveCouponTask((Activity) mContext, o -> {
            BaseCouponInfo baseCouponInfo = (BaseCouponInfo) o;
            if (!Preconditions.isNullOrEmpty(baseCouponInfo.getCouponInfo())) {
                WpToast.l(mContext, "领劵成功");
                setReceiveState(couponId);
            } else {
                WpToast.l(mContext, baseCouponInfo.getMessage());
            }
        }).execute(couponId + "");
    }

    private void setReceiveState(String id) {
        List<ChunnelNewbornBean.EmbeddedBean.ContentBean> data = getData();
        for (int i = 0; i < data.size(); i++) {
            if (id.equals(data.get(i).getCouponId())) {
                data.get(i).setIsReceive(1);
            }
        }
        notifyDataSetChanged();
    }

}
